1 00:00:00,500 --> 00:00:01,250 Welcome. 2 00:00:01,250 --> 00:00:05,270 In this lecture, we'll be exploring object oriented programming and Roblox. 3 00:00:05,300 --> 00:00:08,240 Luigi using tables and meta tables. 4 00:00:08,240 --> 00:00:13,310 If this is a new concept to you or one you're not completely familiar with, don't worry, we're going 5 00:00:13,310 --> 00:00:15,230 to be learning about it more in this lecture. 6 00:00:15,710 --> 00:00:21,830 Object Oriented Programming, or OOP for short, is a programming paradigm that is based on the concept 7 00:00:21,830 --> 00:00:23,000 of objects. 8 00:00:23,000 --> 00:00:29,510 In object oriented programming, an object is an instance of a class, which is a blueprint for creating 9 00:00:29,510 --> 00:00:30,410 objects. 10 00:00:30,410 --> 00:00:36,770 These objects can have attributes or properties and behaviors or methods that define their functionality. 11 00:00:36,980 --> 00:00:40,190 As an example, think of a blueprint for creating a car. 12 00:00:40,190 --> 00:00:44,870 One blueprint exists, but from that blueprint we can make as many cars as we want. 13 00:00:45,080 --> 00:00:50,570 Now in Roblox Lua, we can use tables and meta tables to simulate objects and classes. 14 00:00:50,570 --> 00:00:55,340 By using tables, we can define the class for an object in conjunction with module scripts. 15 00:00:55,340 --> 00:01:00,680 From that point, we can use that table as a helper table or meta table to define the functionality 16 00:01:00,680 --> 00:01:01,610 for the other table. 17 00:01:01,610 --> 00:01:06,230 When we add tables together, subtract tables, call tables, and so on. 18 00:01:06,740 --> 00:01:11,750 The main goal of object oriented programming is to make your code more readable and scalable. 19 00:01:12,660 --> 00:01:17,490 In object oriented programming, there is usually a keyword in the language for creating a class. 20 00:01:17,520 --> 00:01:23,400 However, since Lua is a functional language, there is no class keyword for creating classes, so we 21 00:01:23,400 --> 00:01:27,090 have to imitate classes by using tables instead of studio. 22 00:01:27,120 --> 00:01:31,680 Here I'll give a very basic example of a class and objects we can create from it. 23 00:01:31,950 --> 00:01:38,040 For example, if we wanted an enemy on a game, we could create a variable called enemy and it can be 24 00:01:38,040 --> 00:01:39,390 equal to a table. 25 00:01:39,810 --> 00:01:45,120 Inside of this table, we can set some properties that all enemies in our game would have, like name 26 00:01:45,120 --> 00:01:46,140 health. 27 00:01:46,750 --> 00:01:50,650 Damage, speed and so on. 28 00:01:51,440 --> 00:01:56,600 We can also define a function this class will have that these enemies can use like attack. 29 00:01:58,020 --> 00:02:03,390 This table is now acting as our class, which means we can create a new table. 30 00:02:03,390 --> 00:02:05,310 I'm going to call it Goblin. 31 00:02:06,170 --> 00:02:11,600 And then we can set the meta table for our Goblin cable to be the enemy class. 32 00:02:11,900 --> 00:02:14,720 Because the enemy class is now our helper table. 33 00:02:14,720 --> 00:02:20,300 Anytime we call the goblin table or index the table, or use addition and subtraction on the table. 34 00:02:20,300 --> 00:02:25,490 If our helper table has any defined functionality for those events, they will be used. 35 00:02:25,850 --> 00:02:31,370 So basically, in order for me to access the properties for my goblin that were originally defined within 36 00:02:31,370 --> 00:02:38,030 the class itself, I have to define a meta method in my enemy table for when it is indexed, and meta 37 00:02:38,030 --> 00:02:41,870 method is simply just a special function that defines the behavior for a table. 38 00:02:41,870 --> 00:02:48,620 When a specific context or operation happens, a meta method is declared by creating a key value pair 39 00:02:48,620 --> 00:02:54,860 in our enemy table, and the one I'll be creating here is called underscore underscore index, and I'll 40 00:02:54,860 --> 00:02:57,110 set it to be equal to the class itself. 41 00:02:58,510 --> 00:02:59,680 What did this do? 42 00:02:59,710 --> 00:03:07,030 Well, any time we create a table and the enemy table is attached to it as a helper table, if we were 43 00:03:07,030 --> 00:03:12,190 to index the original table for a key that did not exist within it, then it will go and check the meta 44 00:03:12,190 --> 00:03:15,760 table to see if it has an underscore underscore index meta method. 45 00:03:15,760 --> 00:03:21,130 If it does, then it will go through and check to see if that key exists within the table that was supplied 46 00:03:21,130 --> 00:03:22,210 to the meta method. 47 00:03:22,240 --> 00:03:27,190 This is how we will be able to access the properties that we originally defined within the enemy table. 48 00:03:27,580 --> 00:03:32,080 Now I'm able to access the properties like health and I can set it to whatever I like. 49 00:03:32,440 --> 00:03:37,600 I can also index and call the functions that exist within the meta table two directly on our original 50 00:03:37,600 --> 00:03:38,200 Goblin. 51 00:03:38,800 --> 00:03:43,270 And since I'm calling this function directly on the table, it's automatically passed to the function 52 00:03:43,270 --> 00:03:45,310 as a variable called self. 53 00:03:45,460 --> 00:03:50,590 Self is another keyword that exists within a function that is called with a colon, and because it's 54 00:03:50,590 --> 00:03:54,730 automatically passed, I don't need to define it in the parameters section of my function. 55 00:03:54,730 --> 00:04:01,660 So in my attack function I can print something like a string the concatenated with self dot name, concatenate 56 00:04:01,660 --> 00:04:08,950 it with, attacked with, concatenate it with self dot damage, and then concatenate it with the string 57 00:04:08,950 --> 00:04:09,790 damage. 58 00:04:10,060 --> 00:04:12,370 And we can set the name and damage of our goblin. 59 00:04:15,590 --> 00:04:20,600 Now we can run the game and it should print the goblin attack with 50 damage in the console. 60 00:04:21,230 --> 00:04:21,830 Perfect. 61 00:04:21,830 --> 00:04:23,420 It printed what we expected. 62 00:04:24,440 --> 00:04:28,580 Now, there was another way of creating objects from our class, and that's using a function called 63 00:04:28,580 --> 00:04:33,170 a constructor, which is just a name for a function that creates the objects for us. 64 00:04:33,740 --> 00:04:39,320 Inside of our enemy class, we can define a new function called dot new, and we can pass several arguments 65 00:04:39,320 --> 00:04:44,540 to the function that we will set up for the properties in the object, like the name, the health, 66 00:04:44,540 --> 00:04:46,520 the damage, and the speed. 67 00:04:47,480 --> 00:04:53,180 From that point, we can create a new table within this function, and I'll call it self, and it'll 68 00:04:53,180 --> 00:04:56,270 be equal to the return from the set meta table function. 69 00:04:56,480 --> 00:05:01,490 We will create a new table in this function and attach the enemy class to it as the meta table. 70 00:05:02,350 --> 00:05:05,050 Now we can set all the properties for self. 71 00:05:10,970 --> 00:05:14,810 And then we can return self back to where this function was called. 72 00:05:15,830 --> 00:05:21,680 Now that we have this function defined, we can delete our previous code and use this new function instead. 73 00:05:22,610 --> 00:05:27,980 We can pass the same properties to this constructor like the name health. 74 00:05:28,750 --> 00:05:34,840 Damage and speed, and then we can call the attack function on the variable that gets returned from 75 00:05:34,840 --> 00:05:35,620 this function. 76 00:05:37,070 --> 00:05:40,040 And we should get the exact same output in the console as before. 77 00:05:44,690 --> 00:05:47,030 And just like that, we get what we expected. 78 00:05:49,250 --> 00:05:52,730 As you can see, this is how we can imitate classes and objects in Lua. 79 00:05:52,910 --> 00:05:57,470 In the next lectures, we'll be taking a look at some more core principles of object oriented programming 80 00:05:57,470 --> 00:06:00,410 and how we use module scripts to act as classes. 81 00:06:00,440 --> 00:06:01,970 I hope to see you there.